'use strict';

angular.module('app')
.controller('trajectoryController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                function($scope, $rootScope, $state, $timeout, toaster) {
		$scope.title = '设备在线轨迹';
       
        $scope.loading = false;
        //从本地获取localstorage数据，对权限按钮做隐藏
		var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
		personAuth = personAuth.split(',');
		for(var i = 0;i<personAuth.length;i++){
			if(personAuth[i]=='dm.warnInfo.read'){
				$scope.read = true;
			}
			if(personAuth[i]=='dm.warnInfo.add'){
				$scope.add = true;
			}
			if(personAuth[i]=='dm.warnInfo.update'){
				$scope.update = true;
			}
			if(personAuth[i]=='dm.warnInfo.delete'){
				$scope.dele = true;
			}
		}

		$scope.search = function () {
			$scope.param = {};
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			 }
			$.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : 'dac/dm/trajectory/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
				console.log(result);
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		//初始化列表数据
		$scope.search();
		//自定义模糊查询
		$scope.keySearch = function(){
			$scope.param = {};
			$scope.loading = true;
			var cityId = $('#city').find("option:selected").data('code');
			if(cityId!==undefined){
				$scope.param.cityId = cityId
			}else{
				$scope.param.cityId = ''
			}
			var areaId = $('#area').find("option:selected").data('code');
			if(areaId!==undefined){
				$scope.param.areaId = areaId
			}else{
				$scope.param.areaId = ''
			}
			var streetId = $('#street').find("option:selected").data('code');
			if(streetId!==undefined){
				$scope.param.streetId = streetId
			}else{
				$scope.param.streetId = ''
			}
			var roadId = $('#road').find("option:selected").data('code');
			if(roadId!==undefined){
				$scope.param.roadId = roadId
			}else{
				$scope.param.roadId = ''
			}
			var cuntyId = $('#cunty').find("option:selected").data('code');
			if(cuntyId!==undefined){
				$scope.param.cuntyId = cuntyId
			}else{
				$scope.param.cuntyId = ''
			}
			
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			 }
			$.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : 'dac/dm/trajectory/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		//市
		$scope.getaddress = function () {
                $scope.param = {
                    parentCode:440000//广东省
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                    $scope.city = result.data;
                    console.log(result);
                    $scope.$apply();
                })
            }
            $scope.getaddress();
            //区
            $scope.getarea = function () {
                var parentCode = $('#city').find("option:selected").data('code');//获取value的值
                console.log(parentCode);
                $scope.param = {
                    parentCode:parentCode//广州市区级
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                	if(result&&result!==undefined&&result!==''){
                		$scope.area = result.data;
	                    //解除禁止选择的样式
	                    $('#area').removeAttr('disabled');
	                    console.log(result);
	                    $scope.$apply();
                	}
                    
                })
            }
            //乡镇/街道
            $scope.getStree = function () {
                var parentCode = $('#area').find("option:selected").data('code');//获取value的值
                console.log(parentCode);
                $scope.param = {
                    parentCode:parentCode
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                	if(result&&result!==undefined&&result!==''){
                		$scope.street = result.data;
	                    //解除禁止选择的样式
	                    $('#street').removeAttr('disabled');
	                    console.log(result);
	                    $scope.$apply();
                	}
                })
            }
            //路
            $scope.getRoad = function () {
                var parentCode = $('#street').find("option:selected").data('code');//获取value的值;
                console.log(parentCode)
                $scope.param = {
                    parentCode:parentCode
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                    if(result&&result!==undefined&&result!==''){
                		$scope.road = result.data;
	                    //解除禁止选择的样式
	                    $('#road').removeAttr('disabled');
	                    console.log(result);
	                    $scope.$apply();
                	}
                })
            }
            //小区/村
            $scope.getQuarters = function () {
                var parentCode = $('#road').find("option:selected").data('code');//获取value的值;
                $scope.param = {
                    parentCode:parentCode
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                   if(result&&result!==undefined&&result!==''){
                		$scope.cunty = result.data;
	                    //解除禁止选择的样式
	                    $('#cunty').removeAttr('disabled');
	                    console.log(result);
                	}
                })
            }

		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
} ]);